package com.hainatong.jcmvvmbase.mainhome.fragment;

import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.App;
import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentListViewBinding;
import com.hainatong.jcmvvmbase.mainhome.ComFragmentActivity;
import com.hainatong.jcmvvmbase.mainhome.adapter.SubFunctionAdapter;
import com.hainatong.jcmvvmbase.mainhome.entity.FunctionEntity;
import com.hainatong.mvvmbase.base.NoViewModel;

import java.util.ArrayList;
import java.util.List;

public class WareHouseMgrFragment extends MvvmLazyFragment<FragmentListViewBinding, NoViewModel>
{
    // 1 - 入库 货架   2 - 入库 条码  3 - 出库 条码  4 - 归还 条码
    private int scanType = 0;


    private SubFunctionAdapter adapter;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_list_view;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
          adapter = new SubFunctionAdapter();
          viewDataBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
          viewDataBinding.recyclerview.setHasFixedSize(true);
          viewDataBinding.recyclerview.setAdapter(adapter);
          List<FunctionEntity> funList = new ArrayList<>();
          funList.add(new FunctionEntity(getString(R.string.fun_ruku),""+Constants.FUN_RUKU,R.mipmap.ic_putin));
          funList.add(new FunctionEntity(getString(R.string.fun_chuku),""+Constants.FUN_CHUKU,R.mipmap.ic_ku_out));
          funList.add(new FunctionEntity(getString(R.string.fun_linyongjilu),""+Constants.FUN_LINGYONG,R.mipmap.ic_user_record));
          funList.add(new FunctionEntity(getString(R.string.fun_return),""+Constants.FUN_RETURN,R.mipmap.ic_return));
          adapter.setList( funList );
          adapter.setOnItemClickListener((adapter1, view1, position) -> {

              switch ( Integer.parseInt( adapter.getItem(position).getContentTag() ) )
              {
                  case Constants.FUN_RUKU:
                      scanType = 1;
                      startScanQrcode(scanType);
                      break;
                  case Constants.FUN_CHUKU:
                      scanType = 3;
                      startScanQrcode(scanType);
                      break;
                  case Constants.FUN_LINGYONG:

                      Bundle bundle = new Bundle();
                      bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_LINGYONG);
                      ComFragmentActivity.start( getActivity(),bundle );

                      break;
                  case Constants.FUN_RETURN:
                      scanType = 4;
                      startScanQrcode(scanType);
                      break;
              }
              ToastUtils.showShort( adapter.getItem(position).getTitle() );
          });
    }



    public void startScanQrcode( int type )
    {
        QrManager.getInstance().init( App.getScanQrConfigByType( type ) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                LogUtils.e(TAG, "onScanSuccess: " + result.getContent());
                switch ( scanType )
                {
                    case 1:
                        if( result.getType() == ScanResult.CODE_BAR)
                        {
                            //ToastUtils.showShort( result.getContent() );
                            // 跳转 入库  货架
                            Bundle bundle = new Bundle();
                            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_RUKU);
                            bundle.putString( Constants.SCAN_CONTENT_CODE,  ""+result.getContent() );
                            ComFragmentActivity.start( getActivity(),bundle );
                        }
                        else
                        {
                            ToastUtils.showShort( result.getContent() );
                        }
                        break;
                    case 2:
                        break;
                    case 3:
                        if( result.getType() == ScanResult.CODE_BAR)
                        {
                            // 跳转 出库
                            Bundle bundle = new Bundle();
                            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_CHUKU);
                            bundle.putString( Constants.SCAN_CONTENT_CODE,  ""+result.getContent() );
                            ComFragmentActivity.start( getActivity(),bundle );
                        }
                        else
                        {
                            ToastUtils.showShort( result.getContent() );
                        }
                        break;
                    case 4:
                        if( result.getType() == ScanResult.CODE_BAR)
                        {
                            //ToastUtils.showShort( result.getContent() );
                            // 跳转 归还
                            Bundle bundle = new Bundle();
                            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_RETURN);
                            bundle.putString( Constants.SCAN_CONTENT_CODE,  ""+result.getContent() );
                            ComFragmentActivity.start( getActivity(),bundle );
                        }
                        else
                        {
                            ToastUtils.showShort( result.getContent() );
                        }
                        break;
                }
            }
        });
    }


}
